package com.example.yiwei.crm.view.activity.healthActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetDock;
import com.example.yiwei.crm.data.GetDockData;
import com.example.yiwei.crm.entity.dockingEntity;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;
import com.example.yiwei.crm.view.Class.listviewClass.ListAdapter;

import java.util.List;

public class simpleHealthEducationActivity extends AppCompatActivity implements AutoListView.OnRefreshListener{
    private AutoListView lstv;
    private ListAdapter ladapter;
    dockingEntity dockingEntity;
    private ImageView  img_back, img_add;
    List<dockingEntity> dockingList;
    List<dockingEntity> list;
    int dock_id;
    private Button btn_add_visit,btn_add_activity;
    int docking_id;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<dockingEntity> result = (List<dockingEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    lstv.onRefreshComplete();
                    dockingList.clear();
                    dockingList.addAll(result);
                    break;
            }
//            lstv.setResultSize(result.size());
            ladapter.notifyDataSetChanged();
        }

        ;
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_health_education);
        img_back = (ImageView) findViewById(R.id.health_education_img_back);
//        img_add = (ImageView) findViewById(R.id.health_education_img_plus);
        lstv = (AutoListView) findViewById(R.id.simple_health_education_listview);
//        btn_add_activity= (Button) findViewById(R.id.simple_health_education_btn_add_activity);
        lstv.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        lstv.setDividerHeight(1);
        lstv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                dockDao dockDao = new dockDaoImpl();
                dockingList = dockDao.queryAllDockingMessage(baseClass.query_docking_message_url, null);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ladapter = new ListAdapter(simpleHealthEducationActivity.this, dockingList);
                        lstv.setAdapter(ladapter);
                        ladapter.notifyDataSetChanged();
                        lstv.setOnRefreshListener(simpleHealthEducationActivity.this);
                        lstv.setLoadEnable(false);
                        loadData(AutoListView.REFRESH);
                    }
                });
            }
        }).start();
        lstv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                dock_id=dockingList.get(i-1).getDocking_id();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        dockDao dockDao=new dockDaoImpl();
                        GetDock.create();
                        GetDock.dockingEntity=dockDao.queryDtailedDockingMessage(baseClass.query_detailed_docking_message_url+dock_id,null);
                        if (!GetDock.dockingEntity.toString().equals("")){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Intent intent=new Intent(simpleHealthEducationActivity.this,DetailedHealthEducationActivity.class);
                                    startActivity(intent);
                                }
                            });
                        }
                    }
                }).start();
            }
        });
        img_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(simpleHealthEducationActivity.this, healthEducationAddActivity.class);
                startActivity(intent);
            }
        });
        btn_add_visit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(simpleHealthEducationActivity.this,addVisitActivity.class);
                final List<Integer> list1=ListAdapter.getIDs();
                for (int i=0;i<list1.size();i++){
                    docking_id=list1.get(i);
                }
                if (list1.size()==1){
                    intent.putExtra("docking_id",docking_id);
                    intent.putExtra("TAG","health");
                    startActivity(intent);
                }else if (list1.size()==0){
                    Toast.makeText(simpleHealthEducationActivity.this, "请选择对接人！", Toast.LENGTH_SHORT).show();
                }else if (list1.size()>1){
                    Toast.makeText(simpleHealthEducationActivity.this, "只能选择一个对接人！", Toast.LENGTH_SHORT).show();
                }

            }
        });
        btn_add_activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(simpleHealthEducationActivity.this,addActivity.class);
                final List<Integer> list1=ListAdapter.getIDs();
                for (int i=0;i<list1.size();i++){
                    docking_id=list1.get(i);
                }
                Log.d("lll",list1.toString());
                if (list1.size()==1){
                    intent.putExtra("docking_id",docking_id);
                    startActivity(intent);
                }else if (list1.size()==0){
                    Toast.makeText(simpleHealthEducationActivity.this, "请选择对接人！", Toast.LENGTH_SHORT).show();
                }else if (list1.size()>1){
                    Toast.makeText(simpleHealthEducationActivity.this, "只能选择一个对接人！", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (GetDockData.dockingEntityList!=null){
            dockingList.clear();
            dockingList.addAll(GetDockData.dockingEntityList);
            ladapter.notifyDataSetChanged();
            GetDockData.dockingEntityList=null;
        }
    }
    private void loadData(int refresh) {
        if (refresh==AutoListView.REFRESH){
           UpdateTextTask updateTextTask = new UpdateTextTask();
            updateTextTask.execute();
        }

    }

    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }

    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 0;
            msg.what = what;
            dockDao dockDao = new dockDaoImpl();
            list = dockDao.queryAllDockingMessage(baseClass.query_docking_message_url,null);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {

            msg.obj = list;
            handler.sendMessage(msg);
            super.onPostExecute(integer);
        }
    }
}
